package com.digitalchina.lxnsy.activity.old;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessActivity;
import com.digitalchina.lxnsy.api.BusinessCollect;
import com.digitalchina.lxnsy.api.BusinessStatistics;
import com.digitalchina.lxnsy.api.BusinessTechnologies;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ActivityListVO;
import com.digitalchina.lxnsy.entity.CommentVO;
import com.digitalchina.lxnsy.entity.ImageVO;
import com.digitalchina.lxnsy.entity.TechnologiesCommentVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/7/19 14:19
 * 修改备注：
 * ================================================
 */
public class ActivityDetailActivity extends BaseActivity {

    @Bind(R.id.iv_image)
    SimpleDraweeView ivImage;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_reg_start_time)
    TextView tvRegStartTime;
    @Bind(R.id.tv_start_time)
    TextView tvStartTime;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tv_zbf_name)
    TextView tvZbfName;
    @Bind(R.id.tv_zbf_contacts)
    TextView tvZbfContacts;
    @Bind(R.id.tv_zbf_tel)
    TextView tvZbfTel;
    @Bind(R.id.tv_cbf_name)
    TextView tvCbfName;
    @Bind(R.id.tv_cbf_contacts)
    TextView tvCbfContacts;
    @Bind(R.id.tv_cbf_tel)
    TextView tvCbfTel;
    @Bind(R.id.view)
    View view;
    @Bind(R.id.rl_comment)
    RelativeLayout rlComment;
    @Bind(R.id.tv_comment_name)
    TextView tvCommentName;
    @Bind(R.id.tv_comment_time)
    TextView tvCommentTime;
    @Bind(R.id.tv_comment_content)
    TextView tvCommentContent;
    @Bind(R.id.iv_seller_head)
    SimpleDraweeView ivSellerHead;
    @Bind(R.id.tv_seller_name)
    TextView tvSellerName;
    @Bind(R.id.tv_message)
    TextView tvMessage;
    @Bind(R.id.scrollview)
    ScrollView scrollview;
    @Bind(R.id.bt_sing_up)
    Button btSingUp;
    @Bind(R.id.layout_sign_up)
    LinearLayout layoutSignUp;
    @Bind(R.id.nineGrid)
    NineGridView nineGrid;
    @Bind(R.id.tv_introduce)
    TextView tvIntroduce;
    private ActivityListVO vo;
    private String activityId = "";


    // 接收到活动实体后渲染页面
    private void setViews() {


        // 是否有报名
    /*    if (vo.getIsRegister()!= null && vo.getIsRegister().equals("1")) {
            if ("已取消".equals(vo.getActivityState()) || "已结束".equals(vo.getActivityState())) {
                btSingUp.setText(vo.getActivityState());
                btSingUp.setTextColor(getResources().getColor(R.color.colorWhite));
                btSingUp.setBackgroundColor(getResources().getColor(R.color.colorGray));
            }
            btSingUp.setVisibility(View.VISIBLE);
        } else {
            layoutSignUp.setVisibility(View.GONE);
        }*/


        String[] arr = vo.getImgPath().split(",");

        FrescoUtil.showImageSmall(arr[0], ivImage);
        tvTitle.setText(vo.getActivityName() + "");
        if (vo.getIsFee() == null || vo.getIsFee().equals("0")) {
            tvPrice.setText("免费");
        } else{
            if(vo.getFee()!=null){
                tvPrice.setText("￥" + vo.getFee() + "");

            }else{
                tvPrice.setText("￥"   + "0");
            }

        }

        tvRegStartTime.setText(vo.getRegisterStartTime() + "\n" + vo.getRegisterDeadline());
        tvStartTime.setText(vo.getStartTime() + "\n" + vo.getEndTime());
        tvAddress.setText(vo.getAddress() + "");
        tvZbfName.setText(vo.getSponsorName() + "");
        tvZbfContacts.setText(vo.getSponsorPerson() + "");
        tvZbfTel.setText(vo.getSponsorTel() + "");

        if (!StringUtil.isStrEmpty(vo.getOrganizerName()))
            tvCbfName.setText(vo.getOrganizerName() + "");
        if (!StringUtil.isStrEmpty(vo.getOrganizerPerson()))
            tvCbfContacts.setText(vo.getOrganizerPerson() + "");
        if (!StringUtil.isStrEmpty(vo.getOrganizerTel()))
            tvCbfTel.setText(vo.getOrganizerTel() + "");


        if (vo.getImgPath() != null) {

            ArrayList<ImageInfo> imageInfo = new ArrayList<>();
            String[] imgList = vo.getImgPath().split(",");
            for (int i = 0; i < imgList.length; i++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(JointImageUrl.smallImageUrl(imgList[i]));
                info.setBigImageUrl(JointImageUrl.bigImageUrl(imgList[i]));
                imageInfo.add(info);

                ImageVO im = new ImageVO();
                im.setImage(imgList[i]);
            }
            nineGrid.setAdapter(new NineGridViewClickAdapter(this, imageInfo));
        }

        tvIntroduce.setText(vo.getActivityIntroduce() + "");

    }

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_activity_detail);
        setTitle("活动详情");

        Drawable img = getResources().getDrawable(R.drawable.ic_v_collect);
        img.setBounds(0, 0, img.getMinimumWidth()/2, img.getMinimumHeight()/2);
        mTvRight.setCompoundDrawables(img, null, null, null);
        mTvRight.setText("收藏");
        mTvRight.setVisibility(View.VISIBLE);
    }

    @Override
    public void initMember() {

        if (getIntent().hasExtra("ActivityVO")) {
            vo = (ActivityListVO) getIntent().getSerializableExtra("ActivityVO");
            activityId = vo.getId();
            setViews();
        } else {
            activityId = (String) getIntent().getSerializableExtra("contentId");
            BusinessActivity.showActivity(this, activityId, mHandler);
        }

        BusinessStatistics.browseStatistics(this, "1", activityId, mHandler);

        BusinessTechnologies.getCommentList(this, activityId, "3", 1, 1, mHandler);
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_SHOW_ACTIVITY_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SHOW_ACTIVITY_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_GET_COMMENT_LIST_SUCCESS: // 获取评论
                        List<TechnologiesCommentVO> voList = FastJsonUtil.getListBean(msg.obj.toString(), "body", "list", TechnologiesCommentVO.class);
                        if (voList != null && voList.size() > 0) {
                            tvCommentName.setText(!StringUtil.isStrEmpty(voList.get(0).getMobilePhone()) ? voList.get(0).getMobilePhone() : "匿名用户");
                            tvCommentTime.setText(voList.get(0).getCreateTime());
                            tvCommentContent.setText(voList.get(0).getContent());
                        } else {
                            tvCommentName.setText("暂无评价");
                            tvCommentContent.setVisibility(View.GONE);
                        }
                        break;
                    case MSG.MSG_GET_COMMENT_LIST_FIELD:
                        tvCommentName.setText("暂无评价");
                        tvCommentContent.setVisibility(View.GONE);
                        break;
                    case MSG.MSG_ADD_STORE_SUCCESS:
                        showToast("收藏成功");
                        break;
                    case MSG.MSG_ADD_STORE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        vo = FastJsonUtil.getBeanData(object.toString(), ActivityListVO.class);

        if (vo != null)
            setViews();
    }


    @Override
    public void rightOnClick() {

        if (vo == null)
            return;
        if (noLoginToSkip()){
            BusinessCollect.addStore(ActivityDetailActivity.this, activityId,"2", mHandler);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_zbf_tel, R.id.tv_cbf_tel, R.id.rl_comment, R.id.bt_sing_up})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_zbf_tel:
                break;
            case R.id.tv_cbf_tel:
                break;
            case R.id.rl_comment:
                GotoUtil.gotoActivity(ActivityDetailActivity.this, CommentActivity.class, "CommentVO", new CommentVO(activityId, "3"));
                break;
            case R.id.bt_sing_up:
                if ("1".equals(UserXML.getLoginStatus(ActivityDetailActivity.this))) {
                    if ("已取消".equals(vo.getActivityState()) || "已结束".equals(vo.getActivityState()))
                        return;
                    GotoUtil.gotoActivity(ActivityDetailActivity.this, SingUpActivity.class, "activityId", activityId);
                } else {
                    GotoUtil.gotoActivity(ActivityDetailActivity.this, LoginGuideActivity.class);
                }
                break;
        }
    }
}
